package com.apes.fn.scm.prepare.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.finance.model.PaymentMethodType;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * 缴款单
 * mut
 *
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Prepare.CONST_TABLE_NAME)
public class Prepare extends Model {
    public static final String CONST_TABLE_NAME = "mas_prepare";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "缴款单", padding = 4, prefix = "JK%(company)%(yy)%(month)", useDateRange = true)
    @Comment("编码")
    private String id;  //编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_company_id", referencedColumnName = "id")
    @Comment("平台公司")
    private PartyRole platformCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_type", referencedColumnName = "id")
    @Comment("收款类型")
    private Enumeration paymentType;

    @Column(name = "to_prepare_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("待缴款金额")
    private double toPrepareAmount;

    @Column(name = "prepare_date")
    @Comment("缴款日期")
    private Date toPrepareDate;

    @Column(name = "prepare_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("缴款金额")
    private double prepareAmount;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_method_type", referencedColumnName = "id")
    @Comment("缴款方式")
    private PaymentMethodType type;

    @Column(name = "payment_return", length = 4000)
    @Comment("缴款回执")
    private String paymentReturn;

    @Column(name = "collect_return", length = 4000)
    @Comment("收款回执")
    private String collectReturn;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @Comment("审核日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    private Date approveDate;

    @Column(name = "state")
    @Comment("状态")
    private String state;

    @Comment("缴款平台账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "prepare_account_id", referencedColumnName = "id", nullable = false)
    private BankAccount prepareAccount;

    @Comment("门店回款账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "refund_account_id", referencedColumnName = "id", nullable = false)
    private BankAccount refundAccount;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "advance_uid", referencedColumnName = "id")
    @Comment("生成预支人")
    private PartyRole advanceUid;

    @Comment("生成预支日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "advance_date")
    private Date advanceDate;

    @Column(name = "remarks")
    @Comment("预支单回写单号")
    private String remarks;//预支单回写单号

    @OneToMany(mappedBy = "prepare", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PrepareDetail> prepareDetailSet = new HashSet(); //缴款单明细

    public Set<PrepareDetail> getPrepareDetailSet() {
        if ( prepareDetailSet== null  || prepareDetailSet.isEmpty()) return new HashSet<>();
        for (PrepareDetail item : prepareDetailSet) {
            item.setPrepare(this);
        }
        return prepareDetailSet;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if (this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
